package com.weixing.mall.pay.channel.ali;

import com.alibaba.fastjson.JSON;
import com.weixing.mall.pay.AbstractPayFactory;
import com.weixing.mall.pay.INotify;
import com.weixing.mall.pay.IPayment;
import com.weixing.mall.pay.channel.ali.payment.AliPayApp;
import com.weixing.mall.pay.channel.ali.payment.AliPayPc;
import com.weixing.mall.pay.channel.ali.payment.AliPayWap;
import com.weixing.mall.pay.properties.AliPayProperties;
import com.weixing.mall.pay.properties.PayProperties;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AliPayFactory
 * @Description
 * @Author Small
 * @Date 2019/12/4 19:31
 * @Version 1.0
 **/
public class AliPayFactory extends AbstractPayFactory {

    @Override
    public AliTradeTypeEnum initTradeType(String tradeType) {
        AliTradeTypeEnum tradeTypeEnum = Enum.valueOf(AliTradeTypeEnum.class, tradeType);
        return tradeTypeEnum;
    }

    @Override
    public IPayment getPayment(String tradeType, PayProperties payProperties) {
        AliTradeTypeEnum tradeTypeEnum = initTradeType(tradeType);
        IPayment payment = null;
        AliPayProperties aliPayProperties = payProperties.getAli();
        AliPaySet paySet = convertProp(aliPayProperties);
        switch (tradeTypeEnum) {
            case PC:
                payment = new AliPayPc(paySet);
                break;
            case APP:
                payment = new AliPayApp(paySet);
                break;
            case WAP:
                payment = new AliPayWap(paySet);
                break;
        }
        return payment;
    }

    private AliPaySet convertProp(AliPayProperties properties){
        AliPaySet paySet = new AliPaySet();
        paySet.setAppid(properties.getAppid());
        paySet.setAlipayPublicKey(properties.getAlipayPublicKey());
        paySet.setMerchantPrivateKey(properties.getMerchantPrivateKey());
        paySet.setNotifyUrl(properties.getNotifyUrl());
        return  paySet;
    }

    @Override
    public IPayment getPayment(String tradeType, String payParamJsonStr) {
        AliTradeTypeEnum tradeTypeEnum = initTradeType(tradeType);
        IPayment payment = null;
        AliPaySet paySet = JSON.parseObject(payParamJsonStr,AliPaySet.class);
        switch (tradeTypeEnum) {
            case PC:
                payment = new AliPayPc(paySet);
                break;
            case APP:
                payment = new AliPayApp(paySet);
                break;
            case WAP:
                payment = new AliPayWap(paySet);
                break;
        }
        return payment;
    }

    @Override
    public INotify getNotify(PayProperties payProperties) {
        return new AliNotify(payProperties.getAli());
    }

    @Override
    public INotify getNotify(List<Map<String, Object>> setVoMapList) {
        return new AliNotify(setVoMapList);
    }
}
